package com.cloud.service.sys;

import com.cloud.model.BaseEntity;
import org.apache.log4j.Logger;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public abstract class BaseService<T extends BaseEntity> {
    private static final Logger logger = Logger.getLogger(BaseService.class);

    public abstract JpaRepository getDao();

    public List<T> getAll() {
        List<T> ts = getDao().findAll();
        return ts;
    }

    public T save(T t) {
        return (T) getDao().save(t);
    }

    public T get(String id) {
        Optional<T> t = (getDao().findById(id));
        if (t.isPresent()) {
            return t.get();
        }
        return null;
    }
}
